//
//  JKHorizontalGrideCollectionViewLayout.m
//  JKHorizontalGrideView
//
//  Created by jiukuyinyue on 2017/8/18.
//  Copyright © 2017年 LC. All rights reserved.
//

#import "JKHorizontalGrideCollectionViewLayout.h"

@interface JKHorizontalGrideCollectionViewLayout ()

/**
 预计算 content size 的大小
 */
@property (assign, nonatomic)   CGSize      contentSize;


/**
 预计算所有的 cell 布局属性
 */
@property (strong, nonatomic)   NSMutableArray<UICollectionViewLayoutAttributes *>  *layoutAttributes;

@end

@implementation JKHorizontalGrideCollectionViewLayout

- ( NSMutableArray<UICollectionViewLayoutAttributes *> *)layoutAttributes {
    if(_layoutAttributes == nil) {
        _layoutAttributes = [NSMutableArray array];
    }
    return _layoutAttributes;
}


/**
 获取每页最多有多少个选项
 
 @return 返回选项数
 */
- (NSInteger)maxNumberOfItemsPerPage {
    return self.rowCount * self.columnCount;
}

- (NSInteger)pageCount {
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    NSInteger maxCountPerPage = [self maxNumberOfItemsPerPage];
    return ((count % maxCountPerPage) == 0) ? (count / maxCountPerPage) : ((int)(count * 1.0 / maxCountPerPage) + 1);
}

/**
 准备layout
 prepareLayout ->
 */
- (void)prepareLayout {
    // 清理工作
    [self.layoutAttributes removeAllObjects];
    self.contentSize = CGSizeZero;
    
    // 预先计算好所有的 layout 属性
    // 预计算 contentSize
    // 先要拿到 到底有多少个 item
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    NSInteger maxCountPerPage = [self maxNumberOfItemsPerPage];
    
    // 计算一共多少页
    NSInteger pageCount = [self pageCount];
    
    // 预计算了 contentSize
    self.contentSize = CGSizeMake(pageCount * self.collectionView.frame.size.width, self.collectionView.frame.size.height);
    
    
    // 预计算 每个 cell 的属性大小
    for (NSInteger i = 0; i < count; i++) {
        // 创建索引
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
        
        // 通过索引创建 cell 布局属性
        // UICollectionViewLayoutAttributes 这个内部应该保存 cell 布局以及一些位置信息等等
        UICollectionViewLayoutAttributes *layoutAttribute = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        
        [self.layoutAttributes addObject:layoutAttribute];
    }
    
    NSInteger idx = 0;
    NSLog(@"self.collectionView.frame.size.width = %f", self.collectionView.frame.size.width);
    NSLog(@"self.columnCount = %zd", self.columnCount);
    NSInteger itemWidth = self.collectionView.frame.size.width / self.columnCount;
    NSInteger itemHeight = self.collectionView.frame.size.height / self.rowCount;
    
    // 具体计算每个布局属性到底是多少
    for (NSInteger i = 0; i < pageCount; i++) {
        for (NSInteger j = 0; j < self.rowCount; j++) {
            for (NSInteger k = 0; k < self.columnCount; k++) {
                idx = i * maxCountPerPage + j * self.columnCount + k;
                
                if(idx >= count) {break;}
                
                // 当前获取的布局属性
                UICollectionViewLayoutAttributes *currentLayoutAttribute = self.layoutAttributes[i * maxCountPerPage + j * self.columnCount + k];
                
                CGFloat x = i * self.collectionView.frame.size.width + k * itemWidth;
                CGFloat y = j * itemHeight;
                
                if (j == 1) {//在第二列就减去7像素
                    y = j * itemHeight - 7;
                    currentLayoutAttribute.frame = CGRectMake(x, y, itemWidth, itemHeight - 7);
                }else{
                    currentLayoutAttribute.frame = CGRectMake(x, y, itemWidth, itemHeight);
                }
                
                
            }
            if(idx >= count) {break;}
        }
        if(idx >= count) {break;}
    }
    
}


/**
 返回具体content size
 
 @return size
 */
- (CGSize)collectionViewContentSize {
    return self.contentSize;
}


/**
 在指定区域范围内需要提供cell信息
 
 @param rect 执行区域
 @return 属性列表
 */
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    
    NSMutableArray<UICollectionViewLayoutAttributes *> *visibledAttributes = [NSMutableArray array];
    [self.layoutAttributes enumerateObjectsUsingBlock:^(UICollectionViewLayoutAttributes * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if(CGRectIntersectsRect(obj.frame, rect)) {
            [visibledAttributes addObject:obj];
        }
    }];
    
    return visibledAttributes;
}

@end
